//index.js
//获取应用实例
const garageData = require('./garageData')
const util = require('../../utils/util')
var app = getApp()
let mapkey = 'GIEBZ-WHU3X-WN74E-ZIWQE-FFUC3-NBF4B'
Page({
  data: {
    latitude: '',
    longitude: '',
    scale: 10,   //缩放级别
    markers: [

    ]
  },
  bindregionchange: function (e) {
    if (e.type == "begin") {
      console.log("begin");
    } else if (e.type == "end") {
      console.log("end");
    }
  },
  bindcontroltap: function (e) {
    // 点击事件选项
    switch (e.controlId) {
      // 当点击图标location.png的图标，则触发事件movetoPositioon()
      case 1: this.movetoPosition();

      this.setData({
          scale: 15
        })
        break;
      // 当点击图标use.png的图标，则触发事件showGarage()
      case 2: 
      this.setData({
        isShow: true,
      })
        break;
      case 3:
        this.changeView('3')
        break
      case 4:
        this.changeView('4')
        break
      //   wx.navigateTo({
      //   url: '../logs/logs',
      //   success:(res)=>{
      //       console.log(res);
      //   }
      // })

    }
  },
  //去这里
  goGarage: function (e) {
    let that = this
    let k = e.currentTarget.id
    k = k - 1
    let marker = that.data.markers[k]
    this.setAddressById(marker)

    wx.openLocation({
      latitude: marker.latitude,
      longitude: marker.longitude,
      scale: 8, // 缩放比例
      name: marker.name,
      address: marker.address,
      success(res) {
        console.log(res)
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  //隐藏garagelist
  garlistHidden: function () {
    this.setData({
      isShow: false
    })
  },
  //缩放，放大地图
  changeView(k) {
    let scale = this.data.scale
    if (k === '3') {
      if (this.data.scale <= 20) {
        scale = scale + 1
        this.getAdd()
        this.setData({
          scale
        })
      }
    } else if (k === '4') {
      if (this.data.scale >= 0) {
        scale = scale - 1
        this.setData({
          scale
        })
        this.getAdd()
      }
    } else {
      //this.movetoPosition()
    }
  },

  //移动到当前位置
  movetoPosition: function () {
    this.mapCtx.moveToLocation();
  },
  //设置路径规划的点
  setMarker(e) {
    //e.deatail.markerId
    // console.log(e)
    let that = this
    let k = e.detail.markerId
    k = k - 1
    // this.routePlan(k)
    let marker = that.data.markers[k]

    wx.openLocation({
      latitude: marker.latitude,
      longitude: marker.longitude,
      scale: 8, // 缩放比例
      name: marker.name,
      address: marker.address,
      success(res) {
        console.log(res)
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  //根据经纬度设置address
  setAddressById(marker) {
    let that = this
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + marker.latitude + ',' + marker.longitude + '&key=' + mapkey,
      success(res) {
        that.setData({
          address: res.data.result.address
        })
      }
    })
  },
  //获取当前地址
  getAdd() {
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        })
      }
    })
  },
  //添加标记点
  addMarkers() {
    this.setData({
      markers: app.globalData.sortMarkers
    })
  },
  //事件处理函数
  onLoad: function () {
    console.log('onLoad')
    this.addMarkers()
    wx.getSystemInfo({
        success: (res) => {
          this.setData({
            controls: [{
              id: 1,
              iconPath: '/assets/images/garage/location3.png',
              position: {
                left: res.windowWidth - 50,
                top: res.windowHeight - 100,
                width: 30,
                height: 30
              },
              clickable: true
            },
            {
              id: 2,
              iconPath: '/assets/images/garage/use.png',
              position: {
                left: res.windowWidth / 2 - 45,
                top: res.windowHeight - 100,
                width: 90,
                height: 90
              },
              clickable: true
            },
            {
              id: 3,
              iconPath: '/assets/images/garage/add.png',
              position: {
                left: res.windowWidth - 70,
                top: res.windowHeight - 500,
                width: 40,
                height: 40
              },
              clickable: true
            },
            {
              id: 4,
              iconPath: '/assets/images/garage/cut.png',
              position: {
                left: res.windowWidth - 70,
                top: res.windowHeight - 450,
                width: 40,
                height: 40
              },
              clickable: true
            }
              // {
              //     id: 5,
              //     iconPath: '/assets/images/garage/avatar.png',
              //     position: {
              //       left: res.windowWidth - 68,
              //       top: res.windowHeight - 155,
              //       width: 45,
              //       height: 45
              //     },
              //     clickable: true
              // }
            ]
          })
        }
      })
    console.log(this.data.markers);
  },
  onShow: function () {
    this.data.markers = JSON.parse(JSON.stringify(garageData))
    this.mapCtx = wx.createMapContext("zxMap");
    this.movetoPosition();
  }
})